package com.bjtime.designer.widget

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.AbsoluteLayout
import android.widget.LinearLayout
import com.bjtime.designer.R
import com.bjtime.designer.attributes.BaseAttr
import com.bjtime.designer.attributes.LineAttr
import com.bjtime.designer.attributes.ViewAttr
import com.bjtime.designer.utils
import com.bjtime.designer.view.LineView


class LineView2 @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseLabelView (context, attrs, defStyleAttr) , View.OnTouchListener {

    var des_line_type = 0
    var mLayoutView : View
    var mBorderView : LinearLayout
    var mWBtn       : View
    var mHBtn       : View
    var mLineView   : LineView


    init {
        des_viewtype = ViewAttr.VIEW_TYPE.LINE
        if (attrs != null) {
            var a = context.obtainStyledAttributes(attrs, R.styleable.LineView2)
            des_line_type = a.getInt(R.styleable.LineView2_line2Type, des_line_type)
            a.recycle()
        }


        mLayoutView = LayoutInflater.from(context).inflate(R.layout.layout_designer_label_lineview2, null)
        this.addView(mLayoutView)
        mBorderView = findViewById(R.id.lineview2_label_broderview)
        mWBtn = findViewById(R.id.lineview2_label_right_btn)
        mHBtn = findViewById(R.id.lineview2_label_bottom_btn)

        mLineView = findViewById(R.id.desing_layout_lineview)
        mLineView.setLineStyle(des_line_type)

        mWBtn.setOnTouchListener(this)
        mHBtn.setOnTouchListener(this)
        setSelect(false)

    }


    private var xDelta: Float = 0f
    private var yDelta: Float = 0f

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        val x = event.x
        val y = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                xDelta = x - getWidth()
                yDelta = y - getHeight()
                requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_MOVE -> {
                xDelta = width.toFloat()
                yDelta = height.toFloat()

                val params = getLayoutParams()
                val xDistance = x + xDelta
                val yDistance = y + yDelta
                if (v.id == mWBtn.id) {
                    params.width = xDistance.toInt()
                } else {

                    params.height = yDistance.toInt()

                }
                setLayoutParams(params)
            }
            MotionEvent.ACTION_UP -> requestDisallowInterceptTouchEvent(false)
        }
        return true
    }

    override fun setSelect(select: Boolean) {
        super.setSelect(select)
        if (!select) {
            mBorderView.setBackgroundResource(0)
            mWBtn.visibility = View.GONE
            mHBtn.visibility = View.GONE
        } else {
            if (!des_lock) {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border)
                mWBtn.visibility = View.VISIBLE
                mHBtn.visibility = View.VISIBLE
            } else {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border_locked)
                mWBtn.visibility = View.GONE
                mHBtn.visibility = View.GONE
            }
        }
    }


    fun getFull () : Boolean {
        return if (des_line_type == 0) true else false
    }

    fun setFull (b:Boolean) {
        des_line_type = if (b) 0 else 1
        if (b) {
            mLineView.setLineStyle(des_line_type)
        } else {
            mLineView.setLineStyle(des_line_type)
        }
    }

    override fun getAttr (): LineAttr {

        var attr  = LineAttr ();
        attr.des_id = des_id
        attr.des_left = des_left
        attr.des_top = des_top
        attr.des_width = des_width
        attr.des_height = des_height
        attr.des_lock = des_lock
        attr.des_ptype = des_ptype
        attr.des_rotate = des_rotate
        attr.des_line_style = des_line_type
        attr.des_reversed = des_reversed
        attr.des_viewtype = ViewAttr.VIEW_TYPE.LINE

        attr.prt_bottom = prt_bottom
        attr.prt_right  = prt_right
        attr.prt_top    = prt_top
        attr.prt_left   = prt_left

        return attr
    }

    override fun setAttr (attr: BaseAttr?) {
        attr as LineAttr
        des_id = attr.des_id
        des_left = attr.des_left
        des_top = attr.des_top
        des_width = attr.des_width
        des_height = attr.des_height
        des_lock = attr.des_lock
        des_ptype = attr.des_ptype
        des_rotate = attr.des_rotate
        des_line_type = attr. des_line_style
        des_reversed = attr.des_reversed
        des_viewtype = attr.des_viewtype
    }

    override fun getRect() : Rect {
        var rect = Rect ()
        var params  =  (mBorderView.layoutParams as MarginLayoutParams)
        rect.left   =   left     + mBorderView.paddingLeft + params.leftMargin
        rect.right  =   mLineView.width
        rect.top    =   top      + mBorderView.paddingTop  + params.topMargin
        rect.bottom =   mLineView.height
        return rect

    }

    override fun onMoved(spac: Int) {
        super.onMoved(spac)
        prt_left = (des_left + utils.dp2px(context, 11f)) / spac
        prt_top = (des_top + utils.dp2px(context, 11f)) / spac
        prt_right = mLineView.width / spac //(des_width - utils.dp2px(context, 12f)) / spac
        prt_bottom = (des_height - utils.dp2px(context, 12f)) / spac
    }


    override fun prepared() {

        setFull(if (des_line_type == 0) true else false)
        var params = layoutParams
        if (des_width == 0 || des_height == 0) {
            params.width = utils.dp2px(context, 150f)
            params.height = utils.dp2px(context, 20f)
        } else {
            params.width = des_width
            params.height = des_height
        }
        layoutParams = params
    }
}